package com.cxs.plan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxs.plan.domain.entity.Budget;
import com.cxs.plan.service.BudgetService;
import com.cxs.plan.mapper.BudgetMapper;
import com.cxs.plan.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

/**
* @author Administrator
* @description 针对表【budget(每月预算)】的数据库操作Service实现
* @createDate 2024-05-09 15:45:09
*/
@Service
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, Budget>
    implements BudgetService{

    @Autowired
    private BudgetMapper budgetMapper;
    /**
     * 查询用户预算
     */
    @Override
    public Budget selectUserBudget() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return budgetMapper.selectUserBudget(String.valueOf(year), BaseContext.getCurrentId());
    }
    /**
     * 保存用户预算
     * @param budget 预算
     */
    @Override
    public void saveUserBudget(Budget budget) {
        Budget oldBudget = this.selectUserBudget();
        if(oldBudget != null){
            budget.setId(oldBudget.getId());
            budgetMapper.updateBudgetById(budget);
            return;
        }
        budget.setYear(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
        budget.setUserId(BaseContext.getCurrentId());
        budgetMapper.insertBudget(budget);
    }
}




